package com.jstyle.jclifexd.activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import com.jstyle.jclifexd.NetWork.NetWorkUtil;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.adapter.TeamAdapter;
import com.jstyle.jclifexd.daoManager.UserInfoDaoManager;
import com.jstyle.jclifexd.model.Activitities;
import com.jstyle.jclifexd.model.Activitvtiesmsg;
import com.jstyle.jclifexd.model.Activitvtiesteam;
import com.jstyle.jclifexd.model.Createddata;
import com.jstyle.jclifexd.model.teamData;
import java.util.HashMap;
import java.util.Map;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 加入战队
 */
public class TeamPKActivity extends BaseActivity{
    @BindView(R.id.title)
    Button title;
    @BindView(R.id.teampk_RecyclerView)
    RecyclerView teampk_RecyclerView;
    TeamAdapter teamAdapter;
    Createddata activitiesActivity;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teampk);
        ButterKnife.bind(this);
        if(null!=getIntent()&&null!=getIntent().getSerializableExtra("activitiesActivity")){
            activitiesActivity=(Createddata)getIntent().getSerializableExtra("activitiesActivity");
            title.setText(activitiesActivity.getTitle()==null?"":activitiesActivity.getTitle());
            teampk_RecyclerView.setLayoutManager(new LinearLayoutManager(this));
            teamAdapter=new TeamAdapter();
            teampk_RecyclerView.setAdapter(teamAdapter);
            QueryUserState();
        }
    }


    private void UserJoin(teamData tt){
        Map<String,Object> map=new HashMap<>();
        map.put("teamId",tt.getId());
        map.put("uid",NetWorkUtil.getUserId());
        map.put("nickname",UserInfoDaoManager.getUserByUid(NetWorkUtil.getUserId()).getName());
        NetWorkUtil.getInstance().getJstyleApi().join(map).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Activitvtiesmsg>() {
            @Override
            public void onSubscribe(Disposable d) {}
            @Override
            public void onNext(Activitvtiesmsg activitvtiesData) {
                if(activitvtiesData.getMsgInfo().equals("失败")){
                    showToast(getResources().getString(R.string.joinfailure));
                }else  if(activitvtiesData.getMsgInfo().equals("成功")){
                    showToast(getResources().getString(R.string.joinsuccess));
                    finish();
                }else{
                    showToast(getResources().getString(R.string.joinedteam));
                }
            }
            @Override
            public void onError(Throwable e) { }
            @Override
            public void onComplete() { }
        });

    }





    private void QueryUserState(){
        NetWorkUtil.getInstance().getJstyleApi().getqueryAll().
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Activitvtiesteam>() {
            @Override
            public void onSubscribe(Disposable d) {}
            @Override
            public void onNext(Activitvtiesteam activitvtiesData) {
              teamAdapter.setData(activitvtiesData.getData());
              teamAdapter.setOnItemClickListener(new TeamAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(teamData v, int position) {
                        UserJoin(v);
                    }
                }); }
            @Override
            public void onError(Throwable e) { }
            @Override
            public void onComplete() { }
        });

    }


    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
